import { Component, OnInit, Input, EventEmitter, Output, Host, Inject, forwardRef, ViewChild, ElementRef, AfterViewInit } from '@angular/core';

import { RemoveTableService } from '../../../public/serve/remove-table/remove-table.service';
import { Service } from '../../../server/server'
import { InstitutionService } from '../../services/institution.service';
import { Datatype } from '../institution-table/institution-table.component';
import { Http } from '@angular/http';
declare var $: any;

@Component({
  selector: 'app-institution-delete',
  templateUrl: './institution-delete.component.html',
})
export class InstitutionDeleteComponent implements OnInit {

  constructor(private removeTableService: RemoveTableService, private server: Service, private institutionService: InstitutionService, private http: Http) { }
  private tabVal: Datatype[] = [];

  ngOnInit() {
    this.tabValSubject = this.institutionService.tabValSubject.subscribe(tabVal => {
      this.tabVal = tabVal
    })
  }

  //取消订阅
  public tabValSubject: any;
  ngOnDestroy() {
    if (this.tabValSubject) {
      this.tabValSubject.unsubscribe();
    }
  }

  @Output()
  deleteOutput: EventEmitter<string> = new EventEmitter();

  @Input() selectedIds: string[] = [];
  @Input() selectedFiles: any[] = [];
  @Input() treeData: any[] = [];
  // 选中的机构【】 ，包含chilren的树结构
  items: any[] = [];
  // 确认删
  confirmDel() {
    let url = this.server.baseUrl + 'admin/institutions/deleteInfo';
    // 判断选中的上级机构 
    for (let institutionId of this.selectedIds) {
      this.findInTree(institutionId, this.treeData)
    }
    for (let each of this.items) {
      if (each.children) {
        this.server.prompt = { prompt: true, success: false, fail: true, promptStatement: "选中的机构里含有子机构" }
        this.server.promptclose;
        $("#delete").modal("hide");

        // 清空
        this.items = []
        return
      }
    }
    this.http.post(url, {
      institutionId: this.selectedIds
    }).subscribe(response => {
      let res = response.json()
      if (res.result_code === '0000') {
        this.institutionService.reloadSubject.next('Reload');
        this.server.prompt = { prompt: true, success: true, fail: false, promptStatement: res.message }
        this.server.promptclose;
        $("#delete").modal("hide");
        this.deleteOutput.emit(res.result_code)
      } else {
        this.server.prompt = { prompt: true, success: false, fail: true, promptStatement: res.message }
        this.server.promptclose;
      }
    })



    // this.removeTableService.confirmRemove('institutionId', url).then((res: any) => {
    //   this.deleteOutput.emit(res.ressult_code);
    //   if (res.result_code === '0000') {
    //     this.server.prompt = { prompt: true, success: true, fail: false, promptStatement: res.message }
    //     this.server.promptclose;
    //     $("#delete").modal("hide");
    //   } else {
    //     this.server.prompt = { prompt: true, success: false, fail: true, promptStatement: res.message }
    //     this.server.promptclose;
    //   }
    // }).catch()
  }
  findInTree(institutionId: string, treeData: any[]) {

    for (let tree of treeData) {
      if (tree.institutionId == institutionId) {
        // console.log(tree)
        this.items.push(tree)
        break
      } else if (tree.children) {
        this.findInTree(institutionId, tree.children)
      } else {

      }
    }
  }
}
